package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.log.LogAnno;
import com.itheima.constant.MessageConstant;
import com.itheima.pojo.*;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import com.itheima.pojo.*;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    @LogAnno("回显检查项")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findCheckItems")
    public Result findCheckItems() {
        try {
            List<CheckItem> checkItems = checkGroupService.findCheckItems();
            return Result.success("回显新增检查项成功", checkItems);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("回显新增检查项失败");
        }
    }

    @LogAnno("回显编辑检查项")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findCheckGroupById")
    public Result findCheckGroupById(int id) {
        try {
            List<CheckItem> checkItems = checkGroupService.findCheckItems();
            CheckGroup checkGroup = checkGroupService.findCheckGroupById(id);
            List<Integer> checkItemIds = checkGroupService.findCheckItemIds(id);
            Map<String, Object> data = new HashMap<>();
            data.put("tableData", checkItems);
            data.put("formData", checkGroup);
            data.put("checkitemIds", checkItemIds);
            return Result.success("回显编辑检查项成功", data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("回显编辑检查项失败");
        }
    }

    @LogAnno("添加检查组")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_ADD')")
    @RequestMapping("/addCheckGroup")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.addCheckGroup(checkGroup);
            return Result.success("添加检查组成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加检查组失败");
        }
    }

    @LogAnno("查询分页检查组")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkGroupService.findPage(queryPageBean);
    }

    /**
     * 查询所有检查组
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
        } catch (Exception e) {
            //查询所有检查组失败
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/editCheckGroup")
    public Result editCheckGroup(@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.editCheckGroup(checkGroup);
            return Result.success("添加检查组成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加检查组失败");
        }
    }

    @RequestMapping("/deleteById")
    public Result deleteById(int id) {
        try {
            checkGroupService.deleteById(id);
            return new Result(true, "删除检查组成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除检查组失败");
        }
    }


}
